<html>
<head><meta charset="utf-8"><title>Const Location::caller() · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html">Const Location::caller()</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241658397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Const%20Location%3A%3Acaller%28%29/near/241658397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chase Wilson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html#241658397">(Jun 05 2021 at 22:07)</a>:</h4>
<p>Is there a reason that <a href="https://doc.rust-lang.org/std/panic/struct.Location.html#method.caller"><code>Location::caller()</code></a> isn't a <code>const fn</code>? Ideally I'd like to be able to do something like this if at all possible</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[track_caller]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">NAME</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">caller</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Location</span>::<span class="n">caller</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">const_format</span>::<span class="n">formatcp</span><span class="o">!</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="s">"Called `foo()` @ {}:{}:{}"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">caller</span><span class="p">.</span><span class="n">file</span><span class="p">(),</span><span class="w"></span>
<span class="w">            </span><span class="n">caller</span><span class="p">.</span><span class="n">line</span><span class="p">(),</span><span class="w"></span>
<span class="w">            </span><span class="n">caller</span><span class="p">.</span><span class="n">column</span><span class="p">(),</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="c1">// ...</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="241658512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Const%20Location%3A%3Acaller%28%29/near/241658512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> inquisitivecrystal <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html#241658512">(Jun 05 2021 at 22:10)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/76156">#76156</a> may or may not be helpful.</p>
<p>-Me, a Random Nonexpert Person</p>



<a name="241659015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Const%20Location%3A%3Acaller%28%29/near/241659015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html#241659015">(Jun 05 2021 at 22:25)</a>:</h4>
<p>I don't think that would work even if it was</p>



<a name="241659081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Const%20Location%3A%3Acaller%28%29/near/241659081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html#241659081">(Jun 05 2021 at 22:26)</a>:</h4>
<p>since the <code>const</code> initializer is called by the compiler, not by <code>foo</code></p>



<a name="241659583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Const%20Location%3A%3Acaller%28%29/near/241659583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chase Wilson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Const.20Location.3A.3Acaller().html#241659583">(Jun 05 2021 at 22:42)</a>:</h4>
<p>Putting aside the named constant too then, it could be used within a <code>const fn</code> or an inline <code>const { }</code> block without dealing with problems of depending on the surrounding function</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>